using Api.Bll;
using Api.Model;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("_myAllowSpecificOrigins")]
    public class MessageController : ControllerBase
    {
        // 拉黑用户
        [HttpPost("block")]
        public IActionResult BlockUser(int userId, int blockedUserId)
        {
            if (userId <= 0 || blockedUserId <= 0)
            {
                return BadRequest("Invalid user IDs provided.");
            }
            
            bool success = BllFactory.GetMessageBLL().BlockUser(userId, blockedUserId);
            if (success)
            {
                return Ok("User blocked successfully.");
            }
            return BadRequest("Failed to block user. The user may already be blocked or IDs are invalid.");
        }

        // 解除拉黑
        // 新增 - 解除拉黑
        [HttpPost("unblock")]
        public IActionResult UnblockUser(int userId, int blockedUserId)
        {
            if (userId <= 0 || blockedUserId <= 0)
            {
                return BadRequest("Invalid user IDs provided.");
            }

            bool success = BllFactory.GetMessageBLL().UnblockUser(userId, blockedUserId);
            if (success)
            {
                return Ok("User unblocked successfully.");
            }
            return BadRequest("Failed to unblock user. The user may not be on the block list.");
        }
        
        // 判断用户是否被拉黑
        [HttpGet("isBlocked/{userId}/{blockedUserId}")]
        public bool IsUserBlocked(int userId, int blockedUserId)
        {
            return BllFactory.GetMessageBLL().IsUserBlocked(userId, blockedUserId);
        }

        // 获取用户间的所有私信并标记为已读
        [HttpGet("between/{userId}/{otherUserId}")]
        public List<Message> GetMessagesBetweenUsers(int userId, int otherUserId)
        {
            return BllFactory.GetMessageBLL().GetMessagesBetweenUsersAndMarkAsRead(userId, otherUserId);
        }

        // 获取用户的所有私信
        [HttpGet("user/{userId}")]
        public List<Message> GetMessages(int userId)
        {
            return BllFactory.GetMessageBLL().GetMessages(userId);
        }

        // 
        [HttpGet("singlems/{messageId}")]
        public Message GetSingleMessages(int messageId)
        {
            return BllFactory.GetMessageBLL().GetMessagesById(messageId);
        }

        // 发送私信
        [HttpPost]
        public int? SendMessage([FromBody] Message message)
        {
            return BllFactory.GetMessageBLL().SendMessage(message.SenderId, message.ReceiverId, message.Text);
        }

        // 撤回私信
        [HttpDelete("{messageId}/{userId}")]
        public bool RevokeMessage(int messageId, int userId)
        {
            return BllFactory.GetMessageBLL().RevokeMessage(messageId, userId);
        }

        // 获取所有未读私信
        [HttpGet("unread/{userId}")]
        public List<IGrouping<int, Message>> GetAllUnreadMessagesGroupedBySender(int userId)
        {
            return BllFactory.GetMessageBLL().GetAllUnreadMessagesGroupedBySender(userId);
        }
    }
}
